package cn.weekdragon.gscore.adapter.predicate.impl;

import cn.weekdragon.gscore.adapter.model.EventExt;
import cn.weekdragon.gscore.adapter.predicate.BotPredicate;
import lombok.extern.slf4j.Slf4j;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class PatternPredicate implements BotPredicate {

    public static final String PREDICATE_ATTRIBUTE_KEY_MATCHER = "predicate_attribute_key_matcher";

    private final String pattern;

    public PatternPredicate(String pattern){
        this.pattern = pattern;
    }

    @Override
    public boolean test(EventExt eventExt) {
        boolean find = false;
        try {
            Pattern p = Pattern.compile(pattern);
            Matcher matcher = p.matcher(eventExt.argsText());
            find = matcher.find();
            if(find){
                eventExt.setAttribute(PatternPredicate.PREDICATE_ATTRIBUTE_KEY_MATCHER,matcher);
            }
        } catch (Exception e) {
            log.error("", e);
        }
        return find;
    }
}
